const db = wx.cloud.database();
const app = getApp();
const common = require("../../utils/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderid:null,
    orderObj:null,
    createTime:"",
    role:null,
    approveResult:null,
    workerList: null,
    index:0
  },
  getOrder(){
    wx.showLoading({
      title: '正在加载',
    })
    db.collection("orderList").doc(this.data.orderid).get().then(res=>{
      this.setData({
        orderObj:res.data,
        approveResult:res.data.approveResult,
        createTime:app.formateTime(res.data.createTime)
      })
      this.getWorkerList();
      wx.hideLoading()
    })
  },
  // 取消订单
  delOrder(){
    common.delData("orderList",this.data.orderid,"del")
  },
  // 修改订单信息
  editOrder(){
    wx.navigateTo({
      url: '/pages/editOrder/editOrder?id='+this.data.orderid,
    })
  },
  // 获取审核结果
  getApprove(e){
    this.setData({
      approveResult:e.detail.value
    })
  },
  // 获取维修师傅
  getWorkerList(){
    db.collection("workerList").where({
      type:this.data.orderObj.problem_type
    }).get().then(res=>{
      this.setData({
        workerList:[{name:'请选择维修师傅'}].concat(res.data)
      })
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  // 取消审核
  cancel(){
    wx.navigateBack();
  },
  // 提交审核结果
  submitApprove(res){
    let obj = res.detail.value;
    if (!obj.approveResult) {
      wx.showToast({
        title: '请选择审核结果',
        icon:'none'
      })
    }
    // 审核不通过
   if (obj.approveResult=='0' && !obj.recommendation) {
      wx.showToast({
        title: '请填写审核建议',
        icon:'none'
      })
    }else if(obj.approveResult=='0'){
      db.collection("orderList").doc(this.data.orderid).update({
        data:{
          recommendation:obj.recommendation,
          approveResult:obj.approveResult,
          status:5,
          statusLabel:app.globalData.statusList[5],
        }
      }).then(res=>{
        if (res.stats.updated>0) {
          wx.redirectTo({
            url: '/pages/success/success?type=approve',
          })
        }
      })
    }
    // 审核通过
    if (obj.approveResult=='1' && obj.worker==0) {
      wx.showToast({
        title: '请选择维修师傅',
        icon:'none'
      })
    }else if(obj.approveResult=='1'){
      let worker = this.data.workerList[obj.worker];
      db.collection("orderList").doc(this.data.orderid).update({
        data:{
          workerPhone:worker.phone,
          workerName:worker.name,
          workerNum:worker.num,
          status:1,
          statusLabel:app.globalData.statusList[1],
          approveTime:new Date().getTime(),
          recommendation:null,
          approveResult:obj.approveResult,
        }
      }).then(res=>{
        if (res.stats.updated>0) {
          wx.redirectTo({
            url: '/pages/success/success?type=approve',
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let orderid=options.id;
    this.setData({
      orderid:orderid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      role:app.globalData.role
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrder();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})